package com.baizhi.esdao.impl;

import com.baizhi.entity.Emp;
import com.baizhi.esdao.EmpEsDao;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Repository //标注该类是个dao类 并且让Spring管理
public class EmpEsDaoImpl implements EmpEsDao {
    @Autowired
    private TransportClient client;
    @Override
    public void insert(Emp emp) {
        System.out.println(emp);
       Map<String, Object> map =new HashMap<>();
       map.put("name",emp.getName());
       map.put("age",emp.getAge());
       map.put("content",emp.getContent());
       //将日期格式类型转换为固定格式的字符串
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String bir = df.format(emp.getBir());
        map.put("bir",bir);
       map.put("address",emp.getAddress());
       map.put("name",emp.getName());
       map.put("name",emp.getName());
        IndexResponse indexResponse = client.prepareIndex("ems", "emp", emp.getId() + "").setSource(map).get();

    }

    @Override
    public void delete(int id) {
        client.prepareDelete("ems","emp",id+"").get();
    }

    @Override
    public void update(Emp emp) {

    }

    @Override
    public List<Emp> selectByCon(String con)  {
        SearchResponse response = client.prepareSearch("ems").setTypes("emp")
                .setQuery(QueryBuilders.multiMatchQuery(con, "name", "content"))
                .highlighter(new HighlightBuilder()
                        .field("content").field("name").preTags("<span style='color:red'>").postTags("</span>"))
                .get();
        //在控制台打印获取到的json的结果
        System.out.println(response);
        //将相应的json串转化城List<Emp>
        SearchHits hits = response.getHits();
         List<Emp> empList =new ArrayList<>((int)hits.getTotalHits());
         for (SearchHit hit:hits){
             Map<String, Object> map = hit.getSourceAsMap();
             String idStr = hit.getId();
             Emp emp = new Emp();
             emp.setAddress((String) map.get("address"));
             emp.setAge((Integer) map.get("age"));
             //获取到字符串类型的日期
             String birs = (String) map.get("bir");
             //吧字符串类型的日期转化为Date类型的日期
             DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
             Date bir = null;
             try {
                 bir = df.parse(birs);
             } catch (ParseException e) {
                 e.printStackTrace();
             }
             emp.setBir(bir);
             emp.setContent((String) map.get("content"));
             emp.setId((Integer.parseInt(idStr)));
             emp.setName((String) map.get("name"));

             //把有高亮显示的属性进行替换
             Map<String, HighlightField> highlightFields = hit.getHighlightFields();
             HighlightField nameFiled = highlightFields.get("name");
             //有可能高亮显示的结果里面没有name的key
             if (nameFiled!=null){
                 //获取到高亮显示的name属性里面的第一个数组
                 Text text = nameFiled.fragments()[0];
                 emp.setName(text.toString());
             }
             HighlightField content = highlightFields.get("content");
             //有可能高亮的显示结果里面没有content的key
             if (content!=null) {
                 //获取到高亮显示的name属性里面的第一个数组
                 Text text = content.fragments()[0];
                 emp.setContent(text.toString());
             }
                empList.add(emp);
             }
                return empList;
         }


    }